home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / iutil / gettree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-01-23  |  3.7 KB  |  160 lines

  1. # include    <ingres.h>
  2. # include    <catalog.h>
  3. # include    <tree.h>
  4. # include    <symbol.h>
  5. # include    <sccs.h>
  6.  
  7. SCCSID(@(#)gettree.c    8.2    1/15/85)
  8.  
  9.  
  10. /*
  11. **  GETTREE -- get tree from 'tree' catalog
  12. **
  13. **    This function, given an internal treeid, fetches and builds
  14. **    that tree from the 'tree' catalog.  There is nothing exciting
  15. **    except the mapping of variables, done by mapvars().
  16. **
  17. **    Parameters:
  18. **        treeid -- internal id of tree to fetch and build.
  19. **        init -- passed to 'readqry' to tell whether or not
  20. **            to initialize the query buffer.
  21. **
  22. **    Returns:
  23. **        Pointer to root of tree.
  24. **
  25. **    Side Effects:
  26. **        file activity.  Space in Qbuf is used up.
  27. **
  28. **    Trace Flags:
  29. **        13
  30. */
  31.  
  32. QTREE *
  33. gettree(treerelid, treeowner, treetype, treeid, init)
  34. char    *treerelid;
  35. char    *treeowner;
  36. char    treetype;
  37. int    treeid;
  38. int    init;
  39. {
  40.     register QTREE    *t;
  41.     extern int    relntrrd();
  42.     register int    i;
  43.     extern QTREE    *readqry();
  44.  
  45.     /* initialize relntrrd() for this treeid */
  46.     relntrrd(0, NULL, 0, treerelid, treeowner, treetype, treeid);
  47.  
  48.     /* read and build query tree */
  49.     t = readqry(relntrrd, 0, init);
  50.  
  51.     /* remap varno's to be unique */
  52.     if (!init)
  53.         mapvars(t);
  54.  
  55.     return (t);
  56. }
  57. /*
  58. **  RELNTRRD -- read tree from 'tree' relation
  59. **
  60. **    This looks exactly like the 'pipetrrd' call, except that info
  61. **    comes from the 'tree' talolog instead of from the pipe.  It
  62. **    must be initialized by calling it with a NULL pointer and
  63. **    the segment name wanted as 'treeid'.
  64. **
  65. **    Parameters:
  66. **        dummyx -- a placeholder parameter to make this
  67. **            routine compatible with pb_get.
  68. **        ptr -- NULL -- "initialize".
  69. **            else -- pointer to read area.
  70. **        cnt -- count of number of bytes to read.
  71. **        treerelid -- if ptr == NULL, the relation name
  72. **            associated with the tree; ignored otherwise.
  73. **        treeowner -- if ptr == NULL, the owner of the relation
  74. **            associated with the tree; ignored otherwise.
  75. **        treetype -- if ptr == NULL, the type of the tree
  76. **            (view, prot, etc.); ignored otherwise.
  77. **        treeid -- if ptr == NULL, this is the tree id,
  78. **            otherwise this parameter is not supplied.
  79. **
  80. **    Returns:
  81. **        count of actual number of bytes read.
  82. **
  83. **    Side Effects:
  84. **        activity in database.
  85. **        static variables are adjusted correctly.  Note that
  86. **            this routine can be used on only one tree
  87. **            at one time.
  88. */
  89.  
  90. relntrrd(dummyx, ptr, cnt, treerelid, treeowner, treetype, treeid)
  91. char    *ptr;
  92. int    cnt;
  93. char    *treerelid;
  94. char    *treeowner;
  95. char    treetype;
  96. int    treeid;
  97. {
  98.     static struct tree    trseg;
  99.     static char        *trp;
  100.     static short        seqno;
  101.     static    int        bytesleft;
  102.     register char        *p;
  103.     register int        n;
  104.     register int        i;
  105.     struct tree        trkey;
  106.     TID            tid;
  107.     extern DESC        Treedes;
  108.  
  109.     p = ptr;
  110.     n = cnt;
  111.  
  112.     if (p == NULL)
  113.     {
  114.         /* initialize -- make buffer appear empty */
  115.         trp = &trseg.treetree[sizeof trseg.treetree];
  116.         bytesleft = 0;
  117.         bmove(treerelid, trseg.treerelid, MAXNAME);
  118.         bmove(treeowner, trseg.treeowner, 2);
  119.         trseg.treetype = treetype;
  120.         trseg.treeid = treeid;
  121.         seqno = 0;
  122.         opencatalog("tree", OR_READ);
  123.  
  124. #        ifdef xQTR2
  125.         if (tTf(13, 6))
  126.             printf("relntrrd: n=%.12s o=%.2s t=%d i=%d\n",
  127.                 treerelid, treeowner, treetype, treeid);
  128. #        endif
  129.  
  130.         return (0);
  131.     }
  132.  
  133.     /* fetch characters */
  134.     while (n-- > 0)
  135.     {
  136.         /* check for segment empty */
  137.         if ( bytesleft == 0 )
  138.         {
  139.             /* then read new segment */
  140.             clearkeys(&Treedes);
  141.             setkey(&Treedes, &trkey, trseg.treerelid, TREERELID);
  142.             setkey(&Treedes, &trkey, trseg.treeowner, TREEOWNER);
  143.             setkey(&Treedes, &trkey, &trseg.treetype, TREETYPE);
  144.             setkey(&Treedes, &trkey, &trseg.treeid, TREEID);
  145.             setkey(&Treedes, &trkey, &seqno, TREESEQ);
  146.             seqno++;
  147.             if ((i = getequal(&Treedes, &trkey, &trseg, &tid)) != 0)
  148.                 syserr("relnrdtr: getequal %d", i);
  149.             trp = &trseg.treetree[0];
  150.             bytesleft = sizeof (trseg.treetree);
  151.         }
  152.  
  153.         /* do actual character fetch */
  154.         *p++ = *trp++;
  155.         bytesleft--;
  156.     }
  157.  
  158.     return (cnt);
  159. }
  160.